package Controller;

import Controller.StateImpl.InitState;
import Controller.StateImpl.NewSaleState;
import Controller.StateImpl.PayingState;
import Controller.StateImpl.PurchaseState;
import Modal.ProductCategory;
import Modal.ProductDescription;
import Modal.Sale;

public class POSSystem {

    private Sale curSale;
    private ProductCategory category;
    private State state = InitState.getInstance();

    private static POSSystem instance = new POSSystem();

    private POSSystem() {}

    public static POSSystem getInstance() {
        return instance;
    }

    public void run() {
        while (true) {
            state.doAction();
            System.out.print("");
        }
    }

    public void startUp() {
        category = new ProductCategory();
        state = NewSaleState.getInstance();
    }

    public void makeNewSale() {
        curSale = new Sale();
        state = PurchaseState.getInstance();
    }

    public void endPurchase(){
        curSale.endPurchase();
        state = PayingState.getInstance();
    }

    public void finishASale(){
        curSale.finishASale();
        state = NewSaleState.getInstance();
    }

    public double makePayment(double cash) {
        curSale.makePayment(cash);
        return curSale.getBalance();
    }

    public String enterItem(int id, int qty) {
        ProductDescription product = category.getProduct(id);
        curSale.makeLineItem(product, qty);
        return curSale.getSaleLineInfo();
    }

    public String getSaleLineInfo() {
        return curSale.getSaleLineInfo();
    }

    public double getSaleTotal() {
        return curSale.getTotal();
    }
}